package controllers.cms.blocks;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.type.TypeReference;

import models.cms.BlockDto;
import models.cms.PageDto;
import models.core.user.UserLogDto;
import play.api.templates.Html;
import services.BaseService;
import services.tools.ServiceUrlFormatter;
import controllers.cms.CmsException;
import controllers.cms.TemplateResolver;

public class SocialManagerImpl implements BlockManager {

	@Override
	public Html render(final PageDto p, final BlockDto b) throws CmsException {
		Map<String,ArrayList<UserLogDto>> results = (Map<String,ArrayList<UserLogDto>>) BaseService.get(new TypeReference<Map<String,ArrayList<UserLogDto>>>(){}, ServiceUrlFormatter.format(b.getUrl(), p.getParams()),null).get();
		if(results != null){
			for(String actionId : results.keySet()){
				List<UserLogDto> logs = results.get(actionId);
				if(logs != null && logs.size() > b.getLimit()){
					logs = logs.subList(0, b.getLimit() -1);
				}
				b.addData(""+actionId,logs);
			}
		}
		return TemplateResolver.render(p, b);
	}
}
